package com.sun.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.beans.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * create by Sun
 * 2018/8/6
 */
@Controller
public class photoUpload2 {

    @RequestMapping(value = "/tijiao",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> submit(HttpServletRequest request){
        Map<String,Object> map=new HashMap<>();
        String str=request.getParameter("ssm");
        /**
         *         CommonsMultipartFile fileImg=null;
         * Maven: org.springframework:spring-web:4.3.7.RELEASE] org.springframework.web.multipart.commons public class CommonsMultipartFile extends Object implements MultipartFile, Serializable
         */

        /**
         * 看一下他的构造函数
         * CommonsMultipartResolver 的构造方法传入的是servletContext()
         */
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(
                request.getSession().getServletContext());
        CommonsMultipartFile fileImg=null;
        if(commonsMultipartResolver.isMultipart(request)){
            /**
             * 转成能够获取流的 MultipartHttpServletRequest的 请求
             */
            MultipartHttpServletRequest multipartHttpServletRequest=(MultipartHttpServletRequest)request;
            fileImg= (CommonsMultipartFile) multipartHttpServletRequest.getFile("fileImg");
        }
        try {
            InputStream fileImgInputStream = fileImg.getInputStream();
            File dest=new File("E:/test1/");
            if(!dest.exists()){
                dest.mkdirs();
            }
            File dest1=new File(dest,""+ UUID.randomUUID().toString()+fileImg.getOriginalFilename().substring(fileImg.getOriginalFilename().lastIndexOf(".")));
            byte[] bytes=new byte[1024];
            int len=-2;
            OutputStream fileOutputStream=new FileOutputStream(dest1);
            while ((len=fileImgInputStream.read(bytes))!=-1){
                fileOutputStream.write(bytes,0,len);
            }
         } catch (IOException e) {
            e.printStackTrace();
        }finally {

        }


        System.out.println(fileImg.getOriginalFilename()+"  "+"fileImg");

        System.out.println(str+"xxxxxxxxxx");
        ObjectMapper mapper=new ObjectMapper();
        try {
            Person person1 =(Person) mapper.readValue(str, Person.class);
            System.out.println(person1);

        } catch (IOException e) {

            System.out.println("失败");
            e.printStackTrace();
        }
        map.put("test","ok");
        return map;
    }
}
